package com.gouqi.mall.controller;

import com.gouqi.mall.domain.RegisterInfo;
import com.gouqi.mall.domain.User;
import com.gouqi.mall.service.UserService;
import com.gouqi.mall.utils.CodeUtil;
import com.sun.net.httpserver.HttpServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/***
 * @author :xiaofei
 * @datetime:2020-3-18
 * @description:注册用户控制器
 */
@CrossOrigin
@Api(basePath = "/api/v1", value = "用户注册接口", description = "注册用户")
@Component
@RestController
@RequestMapping(value = "/register")
public class RegisterController extends BaseController{

    @Autowired
    @Qualifier("userServiceImpl")
    private UserService userService;
    //sessionKey
    private final String sessionKey = "RANDOMKEY";
    /**
     * 通过手机号码注册用户
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/user/actionRegister.do",method = RequestMethod.POST, produces = "application/json")
    @ApiOperation(value = "用户手机注册",notes = "一个用户用手机注册成功之后，系统会自动登录为其创建一个token，作为唯一标识")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "byWay",value = "登陆方式",required = true,dataType = "String",defaultValue = "phoneNumber")
    })
    public String registerByPhoneNUmber(@RequestBody @Valid RegisterInfo info, HttpServletRequest request){
        System.out.println("注册手机号：" + info.getPhone() + "密码：" +info.getPassword());
        User user = new User();
        user.setPhone(info.getPhone());
        JSONObject json = new JSONObject();
        if (info.getPassword().equals(info.getConfirmPassword())) {
            user.setPassword(info.getPassword());
        } else {
            json.put("status",404);
            json.put("message","两次密码不一致，请重新输入密码！");
        }
        if (!CodeUtil.verifyCode(request,info.getCode())){
            json.put("status",404);
            json.put("message","验证码错误！");
        }
        Integer id = userService.insertUser(user);
        info.setId(id);
        return json.toString();
    }

    /**
     * 注册用户
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/register",method = RequestMethod.POST, produces = "application/json")
    @ApiOperation(value = "用户注册",notes = "一个用户注册成功之后，系统会自动登录为其创建一个token，作为唯一标识")
    public String register(@Valid @ModelAttribute User user){
        System.out.println("账号：" + user.getAccount() + "密码：" + user.getPassword());
        JSONObject json = new JSONObject();
        Integer userId = userService.getUserIdByAccount(user.getAccount());
        if (userId == null) {
            Integer result = userService.insertUser(user);
            System.out.println("新增用户id:"+result);
            if (result > 0) {
                json.put("status",200);
                json.put("message","新增一个用户成功！");
                json.put("result","success");
                json.put("id",result);
                user.setId(result);
            } else {
                json.put("status",404);
                json.put("message","新增用户失败！");
                json.put("result","failed");
            }
        } else {
            json.put("status",200);
            json.put("message","该用户已经存在，请登录！");
            json.put("result","success");
        }
        return json.toString();
    }
}
